package ug.co.icom.kyazze.healthticket.DataBase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import ug.co.icom.kyazze.healthticket.data.Allergy;
import ug.co.icom.kyazze.healthticket.data.Condition;
import ug.co.icom.kyazze.healthticket.data.Imaging;
import ug.co.icom.kyazze.healthticket.data.Immunization;
import ug.co.icom.kyazze.healthticket.data.Insurance;
import ug.co.icom.kyazze.healthticket.data.LabData;
import ug.co.icom.kyazze.healthticket.data.ListNameID;
import ug.co.icom.kyazze.healthticket.data.Medication;
import ug.co.icom.kyazze.healthticket.data.MyConnections;
import ug.co.icom.kyazze.healthticket.data.Surgery;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Database {
	
	private SQLiteDatabase db;
	Locale locale = Locale.getDefault();
	
	public Database(SQLiteDatabase db)
	{
		this.db = db;
	}
	
	private Date getDateFromString(String dateReceived) {
		SimpleDateFormat  format = new SimpleDateFormat("dd-MM-yyyy", locale);  
		try {  
		    Date date = format.parse(dateReceived);  
		    return date;  
		} catch (Exception e) {  
		    return new Date();
		}
	}

	public void createOrOpenDB()
	{
		db.execSQL("CREATE TABLE IF NOT EXISTS Allergies ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR NOT NULL, DateOfDiagnosis DATE, Severity VARCHAR NOT NULL, "
				+ "Reaction VARCHAR, Occurrence VARCHAR NOT NULL, Comments VARCHAR, IS_Synced VARCHAR DEFAULT 'false');");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Medication ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR NOT NULL, DateStarted DATE, DateEnded DATE, "
				+ "PrescribedBy VARCHAR, Type VARCHAR, HowIsITTaken VARCHAR NOT NULL, "
				+ "Strength INT, StrengthUnit VARCHAR, Quantity INT, Unit VARCHAR, Frequency VARCHAR, "
				+ "Period VARCHAR, Reason VARCHAR, SideEffects VARCHAR, Comments VARCHAR);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Immunization ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR NOT NULL, DateAdministered DATE, DozeSequenceNumber INT,"
				+ " Comments VARCHAR);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS LabData ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR NOT NULL, Date DATE, Results VARCHAR NOT NULL);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Conditions ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR NOT NULL, DateOfDiagnosis DATE, Status VARCHAR, "
				+ "PrimaryProvider VARCHAR, SecondaryProvider VARCHAR, Comments VARCHAR);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Imaging ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Procedure VARCHAR NOT NULL, Date DATE, Comments VARCHAR);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Insurance ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Label VARCHAR NOT NULL, NameOfCard VARCHAR"
				+ "CoverageStartDate DATE, CoverageEndDate DATE, InsuranceCompany VARCHAR, CompanyPhone VARCHAR, "
				+ "PlanName VARCHAR, PlanID VARCHAR, GroupNumber VARCHAR, Subscriber_PolicyID VARCHAR, Comments VARCHAR);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Surgeries ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR NOT NULL, DateOfProcedure DATE, Reason VARCHAR, "
				+ "Facility VARCHAR, Physician VARCHAR, Comments VARCHAR);");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS Connections ("
				+ "ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, profileimage VARCHAR, Uid VARCHAR NOT NULL, Fname VARCHAR, "
				+ "Lname VARCHAR, OtherNames VARCHAR, PhoneNumber VARCHAR, DateOfBirth VARCHAR, Allergies VARCHAR, "
				+ "Medications VARCHAR, Immunization VARCHAR, LabData VARCHAR, Conditions VARCHAR, Imaging VARCHAR, Insurance VARCHAR, Surgeries VARCHAR);");
		
		
	}
	
	public ArrayList<Allergy> getAllAllergies()
	{
		ArrayList<Allergy> allergies = new ArrayList<Allergy>();
		
		Cursor c = db.rawQuery("SELECT * FROM Allergies", null);
		while(c.moveToNext())
		{
			Allergy allergy = new Allergy();
			allergy.ID = c.getInt(c.getColumnIndex("ID"));
			allergy.Name = c.getString(c.getColumnIndex("Name"));
			allergy.DateOfDiagnosis = getDateFromString(c.getString(c.getColumnIndex("DateOfDiagnosis")));
			allergy.Severity = c.getString(c.getColumnIndex("Severity"));
			allergy.Reaction = c.getString(c.getColumnIndex("Reaction"));
			allergy.Occurrence = c.getString(c.getColumnIndex("Occurrence"));
			allergy.Comments = c.getString(c.getColumnIndex("Comments"));
			
			allergies.add(allergy);
		}
		return allergies;
	}
	
	
	
	public ArrayList<Allergy> getUnSyc_AllAllergies()
	{
		ArrayList<Allergy> allergies = new ArrayList<Allergy>();
		String val = "false";
		
		Cursor c = db.rawQuery("SELECT * FROM Allergies WHERE IS_Synced = ?",new String[]{val});
		while(c.moveToNext())
		{
			Allergy allergy = new Allergy();
			allergy.ID = c.getInt(c.getColumnIndex("ID"));
			allergy.Name = c.getString(c.getColumnIndex("Name"));
			allergy.DateOfDiagnosis = getDateFromString(c.getString(c.getColumnIndex("DateOfDiagnosis")));
			allergy.Severity = c.getString(c.getColumnIndex("Severity"));
			allergy.Reaction = c.getString(c.getColumnIndex("Reaction"));
			allergy.Occurrence = c.getString(c.getColumnIndex("Occurrence"));
			allergy.Comments = c.getString(c.getColumnIndex("Comments"));
			allergy.IS_Synced = c.getString(c.getColumnIndex("IS_Synced"));
			
			allergies.add(allergy);
		}
		return allergies;
	}
	
	
	public Allergy getAllergy(String Name)
	{
		Allergy allergy = new Allergy();
		
		Cursor c = db.rawQuery("SELECT * FROM Allergies WHERE Name = ?", new String[]{Name});
		if(c.moveToNext())
		{
			//allergy.ID = c.getInt(c.getColumnIndex("ID"));
			allergy.Name = c.getString(c.getColumnIndex("Name"));
			allergy.DateOfDiagnosis = getDateFromString(c.getString(c.getColumnIndex("DateOfDiagnosis")));
			allergy.Severity = c.getString(c.getColumnIndex("Severity"));
			allergy.Reaction = c.getString(c.getColumnIndex("Reaction"));
			allergy.Occurrence = c.getString(c.getColumnIndex("Occurrence"));
			allergy.Comments = c.getString(c.getColumnIndex("Comments"));
			allergy.IS_Synced = c.getString(c.getColumnIndex("IS_Synced"));
		}
		
		return allergy;
	}
	
	public void insertAllergy(Allergy allergy)
	{
		db.execSQL("INSERT INTO Allergies(Name,DateOfDiagnosis,Severity,Reaction,Occurrence,Comments) Values (?, ?, ?, ?, ?, ?)", new Object[]{
						allergy.Name, allergy.DateOfDiagnosis,
						allergy.Severity, allergy.Reaction, allergy.Occurrence,
						allergy.Comments
				});
	}
	
	public ArrayList<Medication> getAllMedication()
	{
		ArrayList<Medication> medications = new ArrayList<Medication>();
		
		Cursor c = db.rawQuery("SELECT * FROM Medication", null);
		while(c.moveToNext())
		{
			Medication medication = new Medication();
			medication.ID = c.getInt(c.getColumnIndex("ID"));
			medication.Name = c.getString(c.getColumnIndex("Name"));
			medication.DateStarted = getDateFromString(c.getString(c.getColumnIndex("DateStarted")));
			medication.DateEnded = getDateFromString(c.getString(c.getColumnIndex("DateEnded")));
			medication.PrescribedBy = c.getString(c.getColumnIndex("PrescribedBy"));
			medication.Type = c.getString(c.getColumnIndex("Type"));
			medication.HowIsITTaken = c.getString(c.getColumnIndex("HowIsITTaken"));
			medication.Strength = c.getInt(c.getColumnIndex("Strength"));
			medication.StrengthUnit = c.getString(c.getColumnIndex("StrengthUnit"));
			medication.Quantity = c.getInt(c.getColumnIndex("Quantity"));
			medication.Unit = c.getString(c.getColumnIndex("Unit"));
			medication.Frequency = c.getString(c.getColumnIndex("Frequency"));
			medication.Period = c.getString(c.getColumnIndex("Period"));
			medication.Reason = c.getString(c.getColumnIndex("Reason"));
			medication.SideEffects = c.getString(c.getColumnIndex("SideEffects"));
			medication.Comments = c.getString(c.getColumnIndex("Comments"));
			
			medications.add(medication);
		}
		
		return medications;
	}
	
	public Medication getMedication(String ID)
	{
		Medication medication = new Medication();
		
		Cursor c = db.rawQuery("SELECT * FROM Medication WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			medication.ID = c.getInt(c.getColumnIndex("ID"));
			medication.Name = c.getString(c.getColumnIndex("Name"));
			medication.DateStarted = getDateFromString(c.getString(c.getColumnIndex("DateStarted")));
			medication.DateEnded = getDateFromString(c.getString(c.getColumnIndex("DateEnded")));
			medication.PrescribedBy = c.getString(c.getColumnIndex("PrescribedBy"));
			medication.Type = c.getString(c.getColumnIndex("Type"));
			medication.HowIsITTaken = c.getString(c.getColumnIndex("HowIsITTaken"));
			medication.Strength = c.getInt(c.getColumnIndex("Strength"));
			medication.StrengthUnit = c.getString(c.getColumnIndex("StrengthUnit"));
			medication.Quantity = c.getInt(c.getColumnIndex("Quantity"));
			medication.Unit = c.getString(c.getColumnIndex("Unit"));
			medication.Frequency = c.getString(c.getColumnIndex("Frequency"));
			medication.Period = c.getString(c.getColumnIndex("Period"));
			medication.Reason = c.getString(c.getColumnIndex("Reason"));
			medication.SideEffects = c.getString(c.getColumnIndex("SideEffects"));
			medication.Comments = c.getString(c.getColumnIndex("Comments"));
		}
		
		return medication;
	}
	
	public void insertMedication(Medication medication)
	{
		db.execSQL("INSERT INTO Medication Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{
				medication.ID, medication.DateStarted, medication.DateEnded,
				medication.PrescribedBy, medication.Type, medication.HowIsITTaken,
				medication.Strength, medication.StrengthUnit, medication.Quantity, 
				medication.Unit, medication.Frequency, medication.Period,
				medication.Reason, medication.SideEffects, medication.Comments
				});
	}
	
	public ArrayList<Immunization> getAllImmunizations()
	{
		ArrayList<Immunization> immunizations = new ArrayList<Immunization>();
		
		Cursor c = db.rawQuery("SELECT * FROM Immunization", null);
		while(c.moveToNext())
		{
			Immunization immunization = new Immunization();
			immunization.ID = c.getInt(c.getColumnIndex("ID"));
			immunization.Name = c.getString(c.getColumnIndex("Name"));
			immunization.DateAdministered = getDateFromString(c.getString(c.getColumnIndex("DateAdministered")));
			immunization.DozeSequenceNumber = c.getInt(c.getColumnIndex("DozeSequenceNumber"));
			immunization.Comments = c.getString(c.getColumnIndex("Comments"));
			
			immunizations.add(immunization);
		}
		
		return immunizations;
	}
	
	public Immunization getImmunization(String ID)
	{
		Immunization immunization = new Immunization();

		Cursor c = db.rawQuery("SELECT * FROM Immunization WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			immunization.ID = c.getInt(c.getColumnIndex("ID"));
			immunization.Name = c.getString(c.getColumnIndex("Name"));
			immunization.DateAdministered = getDateFromString(c.getString(c.getColumnIndex("DateAdministered")));
			immunization.DozeSequenceNumber = c.getInt(c.getColumnIndex("DozeSequenceNumber"));
			immunization.Comments = c.getString(c.getColumnIndex("Comments"));
		}
		
		return immunization;
	}
	
	public void insertImmunization(Immunization immunization)
	{
		db.execSQL("INSERT INTO Immunization Values (?, ?, ?, ?, ?)", new Object[]{
				immunization.ID, immunization.Name, immunization.DateAdministered,
				immunization.DozeSequenceNumber, immunization.Comments
				});
	}
	
	public ArrayList<LabData> getAllLabData()
	{
		ArrayList<LabData> labdata = new ArrayList<LabData>();
		
		Cursor c = db.rawQuery("SELECT * FROM LabData", null);
		while(c.moveToNext())
		{
			LabData labd = new LabData();
			labd.ID = c.getInt(c.getColumnIndex("ID"));
			labd.Name = c.getString(c.getColumnIndex("Name"));
			labd.LabDataDate = getDateFromString(c.getString(c.getColumnIndex("LabDataDate")));
			labd.Results = c.getString(c.getColumnIndex("Results"));
			
			labdata.add(labd);
		}
		
		return labdata;
	}
	
	public LabData getLabData(String ID)
	{
		LabData labd = new LabData();

		Cursor c = db.rawQuery("SELECT * FROM LabData WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			labd.ID = c.getInt(c.getColumnIndex("ID"));
			labd.Name = c.getString(c.getColumnIndex("Name"));
			labd.LabDataDate = getDateFromString(c.getString(c.getColumnIndex("LabDataDate")));
			labd.Results = c.getString(c.getColumnIndex("Results"));
		}
		
		return labd;
	}
	
	public void insertLabData(LabData labd)
	{
		db.execSQL("INSERT INTO LabData Values (?, ?, ?, ?)", new Object[]{
				labd.ID, labd.Name, labd.LabDataDate,
				labd.Results
				});
	}
	
	public ArrayList<Condition> getAllConditions()
	{
		ArrayList<Condition> conditions = new ArrayList<Condition>();
		
		Cursor c = db.rawQuery("SELECT * FROM Conditions", null);
		while(c.moveToNext())
		{
			Condition condition = new Condition();
			condition.ID = c.getInt(c.getColumnIndex("ID"));
			condition.Name = c.getString(c.getColumnIndex("Name"));
			condition.DateOfDiagnosis = getDateFromString(c.getString(c.getColumnIndex("DateOfDiagnosis")));
			condition.Status = c.getString(c.getColumnIndex("Status"));
			condition.PrimaryProvider = c.getString(c.getColumnIndex("PrimaryProvider"));
			condition.SecondaryProvider = c.getString(c.getColumnIndex("SecondaryProvider"));
			condition.Comments = c.getString(c.getColumnIndex("Comments"));
			
			conditions.add(condition);
		}
		
		return conditions;
	}
	
	public Condition getCondition(String ID)
	{
		Condition condition = new Condition();

		Cursor c = db.rawQuery("SELECT * FROM Conditions WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			condition.ID = c.getInt(c.getColumnIndex("ID"));
			condition.Name = c.getString(c.getColumnIndex("Name"));
			condition.DateOfDiagnosis = getDateFromString(c.getString(c.getColumnIndex("DateOfDiagnosis")));
			condition.Status = c.getString(c.getColumnIndex("Status"));
			condition.PrimaryProvider = c.getString(c.getColumnIndex("PrimaryProvider"));
			condition.SecondaryProvider = c.getString(c.getColumnIndex("SecondaryProvider"));
			condition.Comments = c.getString(c.getColumnIndex("Comments"));
		}
		
		return condition;
	}
	
	public void insertCondition(Condition condition)
	{
		db.execSQL("INSERT INTO Conditions Values (?, ?, ?, ?, ?, ?, ?)", new Object[]{
				condition.ID, condition.Name, condition.DateOfDiagnosis,
				condition.Status, condition.PrimaryProvider, condition.SecondaryProvider,
				condition.Comments
				});
	}
	
	public ArrayList<Imaging> getAllImaging()
	{
		ArrayList<Imaging> imagings = new ArrayList<Imaging>();
		
		Cursor c = db.rawQuery("SELECT * FROM Imaging", null);
		while(c.moveToNext())
		{
			Imaging imaging = new Imaging();
			imaging.ID = c.getInt(c.getColumnIndex("ID"));
			imaging.Procedure = c.getString(c.getColumnIndex("Procedure"));
			imaging.ImagingDate = getDateFromString(c.getString(c.getColumnIndex("ImagingDate")));
			imaging.Comments = c.getString(c.getColumnIndex("Comments"));
			
			imagings.add(imaging);
		}
		
		return imagings;
	}
	
	public Imaging getImaging(String ID)
	{
		Imaging imaging = new Imaging();

		Cursor c = db.rawQuery("SELECT * FROM Imaging WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			imaging.ID = c.getInt(c.getColumnIndex("ID"));
			imaging.Procedure = c.getString(c.getColumnIndex("Procedure"));
			imaging.ImagingDate = getDateFromString(c.getString(c.getColumnIndex("ImagingDate")));
			imaging.Comments = c.getString(c.getColumnIndex("Comments"));
		}
		
		return imaging;
	}
	
	public void insertImaging(Imaging imaging)
	{
		db.execSQL("INSERT INTO Imaging Values (?, ?, ?, ?)", new Object[]{
				imaging.ID, imaging.Procedure, imaging.ImagingDate,
				imaging.Comments
				});
	}
	
	public ArrayList<Insurance> getAllInsurances()
	{
		ArrayList<Insurance> insurances = new ArrayList<Insurance>();
		
		Cursor c = db.rawQuery("SELECT * FROM Insurance", null);
		while(c.moveToNext())
		{
			Insurance insurance = new Insurance();
			insurance.ID = c.getInt(c.getColumnIndex("ID"));
			insurance.Label = c.getString(c.getColumnIndex("Label"));
			insurance.NameOfCard = c.getString(c.getColumnIndex("NameOfCard"));
			insurance.CoverageStartDate = getDateFromString(c.getString(c.getColumnIndex("CoverageStartDate")));
			insurance.CoverageEndDate = getDateFromString(c.getString(c.getColumnIndex("CoverageEndDate")));
			insurance.InsuranceCompany = c.getString(c.getColumnIndex("InsuranceCompany"));
			insurance.CompanyPhone = c.getString(c.getColumnIndex("CompanyPhone"));
			insurance.PlanName = c.getString(c.getColumnIndex("PlanName"));
			insurance.PlanID = c.getString(c.getColumnIndex("PlanID"));
			insurance.GroupNumber = c.getString(c.getColumnIndex("GroupNumber"));
			insurance.Subscriber_PolicyID = c.getString(c.getColumnIndex("Subscriber_PolicyID"));
			insurance.Comments = c.getString(c.getColumnIndex("Comments"));
			
			insurances.add(insurance);
		}
		
		return insurances;
	}
	
	public Insurance getInsurance(String ID)
	{
		Insurance insurance = new Insurance();

		Cursor c = db.rawQuery("SELECT * FROM Insurance WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			insurance.ID = c.getInt(c.getColumnIndex("ID"));
			insurance.Label = c.getString(c.getColumnIndex("Label"));
			insurance.NameOfCard = c.getString(c.getColumnIndex("NameOfCard"));
			insurance.CoverageStartDate = getDateFromString(c.getString(c.getColumnIndex("CoverageStartDate")));
			insurance.CoverageEndDate = getDateFromString(c.getString(c.getColumnIndex("CoverageEndDate")));
			insurance.InsuranceCompany = c.getString(c.getColumnIndex("InsuranceCompany"));
			insurance.CompanyPhone = c.getString(c.getColumnIndex("CompanyPhone"));
			insurance.PlanName = c.getString(c.getColumnIndex("PlanName"));
			insurance.PlanID = c.getString(c.getColumnIndex("PlanID"));
			insurance.GroupNumber = c.getString(c.getColumnIndex("GroupNumber"));
			insurance.Subscriber_PolicyID = c.getString(c.getColumnIndex("Subscriber_PolicyID"));
			insurance.Comments = c.getString(c.getColumnIndex("Comments"));
		}
		
		return insurance;
	}
	
	public void insertInsurance(Insurance insurance)
	{
		db.execSQL("INSERT INTO Insurance Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{
				insurance.ID, insurance.Label, insurance.NameOfCard,
				insurance.CoverageStartDate, insurance.CoverageEndDate, insurance.InsuranceCompany,
				insurance.CompanyPhone, insurance.PlanName,
				insurance.PlanID, insurance.GroupNumber, insurance.Subscriber_PolicyID,
				insurance.Comments
				});
	}
	
	public ArrayList<Surgery> getAllSurgeries()
	{
		ArrayList<Surgery> surgeries = new ArrayList<Surgery>();
		
		Cursor c = db.rawQuery("SELECT * FROM Surgeries", null);
		while(c.moveToNext())
		{
			Surgery surgery = new Surgery();
			surgery.ID = c.getInt(c.getColumnIndex("ID"));
			surgery.Name = c.getString(c.getColumnIndex("Name"));
			surgery.DateOfProcedure = getDateFromString(c.getString(c.getColumnIndex("DateOfProcedure")));
			surgery.Reason = c.getString(c.getColumnIndex("Reason"));
			surgery.Facility = c.getString(c.getColumnIndex("Facility"));
			surgery.Physician = c.getString(c.getColumnIndex("Physician"));
			surgery.Comments = c.getString(c.getColumnIndex("Comments"));
			
			surgeries.add(surgery);
		}
		
		return surgeries;
	}
	
	public Surgery getSurgery(String ID)
	{
		Surgery surgery = new Surgery();

		Cursor c = db.rawQuery("SELECT * FROM Surgeries WHERE ID = ?", new String[]{ID});
		if(c.moveToNext())
		{
			surgery.ID = c.getInt(c.getColumnIndex("ID"));
			surgery.Name = c.getString(c.getColumnIndex("Name"));
			surgery.DateOfProcedure = getDateFromString(c.getString(c.getColumnIndex("DateOfProcedure")));
			surgery.Reason = c.getString(c.getColumnIndex("Reason"));
			surgery.Facility = c.getString(c.getColumnIndex("Facility"));
			surgery.Physician = c.getString(c.getColumnIndex("Physician"));
			surgery.Comments = c.getString(c.getColumnIndex("Comments"));
		}
		
		return surgery;
	}
	
	public void insertSurgeries(Surgery surgery)
	{
		db.execSQL("INSERT INTO Surgeries Values (?, ?, ?, ?, ?, ?, ?)", new Object[]{
				surgery.ID, surgery.Name, surgery.DateOfProcedure,
				surgery.Reason, surgery.Facility, surgery.Physician,
				surgery.Comments
				});
	}
	
	
	public void insertConnection(MyConnections connections)
	{
		db.execSQL("INSERT INTO Connections(profileimage, Uid,Fname,Lname,OtherNames,PhoneNumber,DateOfBirth,Allergies,Medications,Immunization,LabData,Conditions, Imaging, Insurance, Surgeries) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{
				connections.profileimage, connections.uid,	connections.firstname, connections.lastname,
				connections.othernames, connections.phonenumber, connections.dateofbirth,
				connections.allergies,connections.medication,connections.immunization, connections.labdata, connections.conditions, connections.imaging, connections.insurance, connections.surgeries
		});
		
	
	}
	
	
	
	public ArrayList<MyConnections> getAllConnections()
	{
		ArrayList<MyConnections> myconnections = new ArrayList<MyConnections>();
		
		Cursor c = db.rawQuery("SELECT * FROM Connections", null);
		while(c.moveToNext())
		{
			MyConnections connection = new MyConnections();
			connection.profileimage = c.getString(c.getColumnIndex("profileimage"));
			connection.uid = c.getString(c.getColumnIndex("Uid"));
			connection.firstname = c.getString(c.getColumnIndex("Fname"));
			connection.lastname = c.getString(c.getColumnIndex("Lname"));
			connection.othernames = c.getString(c.getColumnIndex("OtherNames"));
			connection.phonenumber = c.getString(c.getColumnIndex("PhoneNumber"));
			connection.dateofbirth = c.getString(c.getColumnIndex("DateOfBirth"));
			connection.allergies = c.getString(c.getColumnIndex("Allergies"));
			connection.medication = c.getString(c.getColumnIndex("Medications"));
			connection.immunization = c.getString(c.getColumnIndex("Immunization"));
			connection.labdata = c.getString(c.getColumnIndex("LabData"));
			connection.conditions = c.getString(c.getColumnIndex("Conditions"));
			connection.imaging = c.getString(c.getColumnIndex("Imaging"));
			connection.insurance = c.getString(c.getColumnIndex("Insurance"));
			connection.surgeries = c.getString(c.getColumnIndex("Surgeries"));
			
			myconnections.add(connection);
		}
		return myconnections;
	}
	
	
	
	
	public ArrayList<ListNameID> getItems(String categoryName)
	{
		ArrayList<ListNameID> items = new ArrayList<ListNameID>();
		
		Cursor c = db.rawQuery(getItemQuery(categoryName), null);
		while(c.moveToNext())
		{
			ListNameID item = new ListNameID();
			item.ID = c.getInt(c.getColumnIndex("ID"));
			item.Name = c.getString(c.getColumnIndex("Name"));
			
			items.add(item);
		}
		
		return items;
	}

	private String getItemQuery(String categoryName) {
		if(categoryName.equals("Allergies"))
		{
			return "SELECT ID, Name FROM Allergies";
		}
		else if(categoryName.equals("Medication"))
		{
			return "SELECT ID, Name NAME FROM Medication";
		}
		else if(categoryName.equals("Immunization"))
		{
			return "SELECT ID, Name FROM Immunization";
		}
		else if(categoryName.equals("LabData"))
		{
			return "SELECT ID, Name FROM LabData";
		}
		else if(categoryName.equals("Conditions"))
		{
			return "SELECT ID, Name FROM Conditions";
		}
		else if(categoryName.equals("Imaging"))
		{
			return "SELECT ID, Procedure AS Name FROM Imaging";
		}
		else if(categoryName.equals("Insurance"))
		{
			return "SELECT ID, Label AS Name FROM Insurance";
		}
		else if(categoryName.equals("Surgeries"))
		{
			return "SELECT ID, Name FROM Surgeries";
		}
		else
		{
			return "";
		}
	}
}
